package com.huang.usercenter.controller;

import com.huang.usercenter.Exception.BusinessException;
import com.huang.usercenter.common.BaseResponse;
import com.huang.usercenter.common.ErrorCode;
import com.huang.usercenter.common.ResultUtil;
import com.huang.usercenter.model.domain.Request.UserLoginRequest;
import com.huang.usercenter.model.domain.Request.UserRegisterRequest;
import com.huang.usercenter.model.domain.User;
import com.huang.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.huang.usercenter.constant.UserConstant.ADMIN_ROLE;
import static com.huang.usercenter.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("userRegister")
    public BaseResponse<Long> UserRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount, password, checkPassword, planetCode)) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long id = userService.userRegister(userAccount, password, checkPassword, planetCode);
        return ResultUtil.success(id);
    }

    @PostMapping("userLogin")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String password = userLoginRequest.getPassword();
        if (StringUtils.isAnyBlank(userAccount, password)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        User user = userService.userLogin(userAccount, password, request);
        return ResultUtil.success(user);
    }

    /**
     * 校验还没有做
     *
     * @param user 前端传来用来根据ID修改的用户
     * @return
     */
    @PutMapping("updateById")
    public BaseResponse<Boolean> updateById(@RequestBody User user, HttpServletRequest request) {
        if (!isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        Long userId = user.getId();
        if (userId == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        Boolean result = userService.updateById(user);
        return ResultUtil.success(result);
    }

    /**
     * 校验还没有做
     *
     * @param user 前端传来用来保存的用户
     * @return
     */
    @PostMapping("saveUser")
    public BaseResponse<Boolean> saveUser(@RequestBody User user, HttpServletRequest request) {
        if (!isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        Boolean result = userService.saveUser(user);
        return ResultUtil.success(result);
    }

    @GetMapping("search")
    public BaseResponse<List<User>> searchUsers(String username, HttpServletRequest request) {
        if (!isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        List<User> userList = userService.SearchUsers(username);
        return ResultUtil.success(userList);
    }

    @DeleteMapping("deleteById")
    public BaseResponse<Boolean> deleteById(@RequestBody long id, HttpServletRequest request) {
        if (!isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (id < 0){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        boolean deleteResult = userService.removeById(id);
        return ResultUtil.success(deleteResult);
    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        return ResultUtil.success(user);
    }

    @PostMapping("/logout")
    public BaseResponse<Integer> logout(HttpServletRequest request) {
        int logoutResult = userService.logout(request);
        return ResultUtil.success(logoutResult);
    }


    /**
     * 是否为管理员
     *
     * @param request
     * @return true 是； false 不是
     */
    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        return user != null && user.getUserRole() == ADMIN_ROLE;
    }
}
